﻿using CMS.Auth;
using CMS.Excep;
using CMS.Model;
using CMS.ServiceProvider;
using Microsoft.AspNetCore.Mvc;

namespace CMS.Controllers
{
    public class ScheduleController : BaseController<ScheduleModel, ScheduleServiceProvider>
    {
        [HttpPost, MyAuthorize(true)]

        public virtual ResponseData<ScheduleModel> InitFromTemplate(dynamic data)
        {
            var cls = data.dseClass?.ToString();
            var start = data.start?.ToString();
            var end = data.end?.ToString();
            var _s = DateTime.Parse(start);
            var _e = DateTime.Parse(end);
            var result = new ResponseData<ScheduleModel>(serviceProvider.ConvertFromTemplate(cls, _s, _e));
            return result;
        }
        [HttpPost, MyAuthorize(true)]

        public virtual ResponseData<string> Exchange(dynamic data)
        {
            var one = data.one?.ToString();
            var other = data.other?.ToString();
            serviceProvider.ExchangeSchedule(one, other);
            var result = new ResponseData<string>();
            return result;
        }
        [HttpGet, MyAuthorize(false)]

        public virtual ResponseData<ScheduleModel> ListByClass(string dseClass, string start, string end)
        {
            var result = new ResponseData<ScheduleModel>(serviceProvider.ListByClass(dseClass, start, end));
            return result;
        }

        [HttpPost, MyAuthorize(true)]

        public virtual ResponseData<string> RemoveByClass(dynamic data)
        {
            string dseClass= data.dseClass!.ToString();
            string start = data.start!.ToString();
            string end = data.end!.ToString();
            serviceProvider.RemoveByDseClass(dseClass, start, end);
            var result = new ResponseData<string>("操作成功");
            return result;
        }
        [HttpGet, MyAuthorize(false)]

        public virtual ResponseData<ScheduleModel> ListByTeacher(string teacher, string start, string end)
        {
            var result = new ResponseData<ScheduleModel>(serviceProvider.ListByTeacher(teacher, start, end));
            return result;
        }
        [HttpPost, MyAuthorize(true)]

        public virtual ResponseData<string> Move(dynamic data)
        {
            var from = data.from?.ToString();
            var to = data.to?.ToString();
            if (!DateTime.TryParse(from, out DateTime dt1) || !DateTime.TryParse(to, out DateTime dt2))
                throw new MyArgException("日期格式有误");
            serviceProvider.Move(from,to);
            var result = new ResponseData<string>();
            return result;
        }
    }
}
